import { Component, AfterViewInit, ViewChild } from '@angular/core';
import { NavController, NavParams, InfiniteScroll, Refresher, Content, Img, ModalController } from 'ionic-angular';
import { News } from '../../providers/news-data/news';
import { NewsDataProvider } from '../../providers/news-data/news-data';
import { updateImgs } from 'ionic-angular/components/content/content';
import { NewsDetailPage } from '../news-detail/news-detail';
import { InputTaskPage } from '../input-task/input-task';
import { Storage } from '@ionic/storage';


@Component({
  selector: 'page-department-news',
  templateUrl: 'department-news.html',
})
export class DepartmentNewsPage implements AfterViewInit {
  @ViewChild(Content) _content: Content;

  news: News[] = [];
  flag: boolean;
  leval: string;


  private num: number = 5;//每页显示记录数
  private curPage: number = 1;//当前页shu
  private hasMore: boolean = true;//是否有更多数据

  constructor(
    public navCtrl: NavController,
    public navParams: NavParams,
    public modalCtrl: ModalController,
    public newsDataProvider: NewsDataProvider,
    storage: Storage) {
  }

  ionViewDidLoad() {
    this.getNews(this.num, this.curPage);
    this.leval = localStorage.getItem("leval");
     if (this.leval == '0') {
      this.flag = false;

    } else {
      this.flag = true;
    }
console.log(this.flag);

    }
   

    getNews(num: number, page : number): void {
      this.newsDataProvider.getNews(num, page).subscribe(news => {

        // this.news = news;
        // 保留原有数据
        if (news.length > 0) {
          this.news = this.news.concat(news);
        } else {
          this.hasMore = false;
        }
      });
    }

    /**
     * 新增新闻
     */
    addNews(){
      let addModal = this.modalCtrl.create(InputTaskPage);

      addModal.onDidDismiss((news: News) => {
        if (news) {
          this.news.push(news);
        }
      });

      addModal.present();



    }

    /**
     * 向下滚动加载更多数据
     * @param infiniteScroll 
     */
    doInfinite(infiniteScroll: InfiniteScroll): void {
      if(!this.hasMore) {
      infiniteScroll.complete();
      return;
    }

    setTimeout(
      () => {
        this.curPage += 1;
        this.getNews(this.num, this.curPage);
        infiniteScroll.complete();
      }, 500
    );
  }


  /**
   *  下拉数据刷新
   * @param refresher
   */
  doRefresh(refresher: Refresher): void {
    setTimeout(() => {
      this.curPage = 1;
      this.getNews(this.num, this.curPage);
      refresher.complete();
    }, 1000);
  }

  /**
   * 详情页
   */
  itemTapped(news: News): void {

    this.navCtrl.push(NewsDetailPage, { news: news });
  }


  /**
   *搜索
   * @param event 
   */
  // searchNews(event: any) {
  //   let query: string = event.target.value;
  //   if (query && query.length > 0) {
  //     query = query.trim();
  //     this.newsDataProvider.getNews();
  //   }
  // }

  /**
   * 重新显示结果集
   * @param event 
   */
  resetNews(event: any) {
    this.curPage = 1;
    // this.isSearch = false;
    this.news = [];
    this.getNews(this.num, this.curPage);
  }


  // 修补在Virtual-Scroll中的图片显示问题
  ngAfterViewInit(): void {
    //Called after ngAfterContentInit when the component's view has been initialized. Applies to components only.
    //Add 'implements AfterViewInit' to the class.

    if (this._content) {
      this._content.imgsUpdate = () => {
        if (this._content._scroll.initialized && this._content._imgs.length &&
          this._content.isImgsUpdatable()) {
          this._content._imgs.forEach((img: Img) => { img._rect = null });
          updateImgs(this._content._imgs, this._content._cTop * -1,
            this._content.contentHeight, this._content.directionY, 1400, 400);
        }
      }
    }

  }







}
